//
//  MVViewController.m
//  艾听歌
//
//  Created by dllo on 16/5/28.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "MVViewController.h"
#import "MvTableViewCell.h"
#import "SingerViewController.h"
#import "MVChildModel.h"
#import "STKAudioPlayer.h"
#import "MoviePlayerViewController.h"
#import "MVdetialViewController.h"

#import "SongModel.h"

@interface MVViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, assign) BOOL isUpLoad;



@end

@implementation MVViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.view.backgroundColor = [UIColor yellowColor];
    self.navigationController.navigationBar.dk_barTintColorPicker = DKColorPickerWithRGB(0xFF6EB4, 0x9C9C9C);
    self.view.dk_backgroundColorPicker = DKColorPickerWithRGB(0xffffff, 0x9c9c9c);
    self.dataArr = [NSMutableArray array];
    [self createTableView];
//    [self dataHandle:self.page];
    [self addHeader];
    [self addFooter];
    
}
- (void)addHeader
{
    __block MVViewController *root = self;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        root.page = 1;
        root.isUpLoad = NO;
        [root dataHandle:root.page];
        
    }];
    [self.tableView.mj_header beginRefreshing];
}
- (void)addFooter
{
    __block MVViewController *root = self;
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        //进入刷新状态就会回调这个Block
        
        root.isUpLoad = YES;//标记为上拉操作
        root.page++;
        [root dataHandle:root.page];
        
        
    }];

}



- (void)dataHandle:(NSInteger)nectCoure
{
    
        AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    
    NSString *str = [NSString stringWithFormat:@"http://api.dongting.com/song/singer/%@/songs?page=%ld&size=50&app=ttpod&v=v9.0.1.2016050614&user_id=0&mid=iPhone7%%2C2&f=f320&s=s310&imsi=&hid=&splus=9.3.1&active=1&net=2&openudid=5e6106cc8d5bcf35f60a2997c79a00bb5a7c631f&idfa=ABDADD65-0877-4840-B14F-DFC8D1114AB0&utdid=VzR0dwnWYs8DANbYJ8SsFzOU&alf=(null)&bundle_id=com.ttpod.music&latitude=-1&longtitude=-1&access_token=(null)&vc=9000100",self.mId, nectCoure];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:str parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
         [netWorkManager stopMonitoring];
        if (self.isUpLoad == NO) {
            
            //说明是下拉,就要清空数组中的数据
            [self.dataArr removeAllObjects];
        }

        NSMutableArray *array = [responseObject objectForKey:@"data"];
        
        for (NSDictionary *dic in array) {
            SongModel *model = [[SongModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            if (model.mvList.count != 0) {
                
            [self.dataArr addObject:model];
                
            }
            
        }
        
        if (nectCoure == 1) {
            
            [self.dataArr removeObjectAtIndex:0];
            
        }
        
        
        [self.tableView.mj_footer endRefreshing];
        [self.tableView.mj_header endRefreshing];

        
        [self.tableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

    }];
}

- (void)createTableView{
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:(UITableViewStylePlain)];
    self.tableView.dk_backgroundColorPicker = DKColorPickerWithRGB(0xffffff, 0x9c9c9c);

    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.rowHeight = 100;
    [self.view addSubview:self.tableView];
    [self.tableView registerClass:[MvTableViewCell class] forCellReuseIdentifier:@"mvCell"];
    
   
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MvTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"mvCell" forIndexPath:indexPath];
    cell.dk_backgroundColorPicker = DKColorPickerWithRGB(0xffffff, 0x9c9c9c);
    SongModel *mvModel = self.dataArr[indexPath.row];
    cell.singerNameLabel.text = mvModel.singerName;
    cell.nameLabel.text = mvModel.name;
    MVChildModel *smallModel = [mvModel.mvList firstObject];
    [cell.imgView sd_setImageWithURL:[NSURL URLWithString:smallModel.picUrl]];
//    NSLog(@"%@", mvModel.mvList);
   
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
   
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MVdetialViewController *detailVC = [[MVdetialViewController alloc] init];
    SongModel *model = self.dataArr[indexPath.row];

    detailVC.MVurl = model.mvList;
    [self.navigationController pushViewController:detailVC animated:YES];
    
}



@end
